package jp.co.ricoh.melody.presenters;

import android.content.Context;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import java.io.File;

import jp.co.ricoh.melody.models.ISoundRecyclerViewModel;
import jp.co.ricoh.melody.models.SDCardSoundRecyclerViewModelImpl;
import jp.co.ricoh.melody.models.entities.SoundData;
import jp.co.ricoh.melody.utils.LogUtils;
import jp.co.ricoh.melody.views.adapters.ISDCardSoundRecycleView;
import jp.co.ricoh.melody.views.adapters.ISoundRecycleView;
import jp.co.ricoh.melody.views.adapters.SDCardSoundRecycleView;

/**
 * Created by tanyajie on 2016/11/17.
 */

public class SDCardSoundRecyclerViewPresenterImpl extends SDCardSoundRecycleView.Adapter<SDCardSoundRecycleView.SoundItemViewHolder> implements ISoundRecyclerViewPresenter {
    private Context mContext;
    private ISDCardSoundRecycleView mView;
    private ISoundRecyclerViewModel mModel;
    private SoundData mCurrentSelectedItem;
    private MediaPlayer mMediaPlayer;
    private AudioManager mAudioManager;

    @Override
    public void onCreate(Context context, ISoundRecycleView view, SoundData soundData) {
    }
    public void deleteSoundItem(int index){}

    @Override
    public void onCreate(Context context, ISDCardSoundRecycleView view, SoundData soundData) {
        mCurrentSelectedItem = soundData;
        mContext = context;
        mView = view;
        mModel = new SDCardSoundRecyclerViewModelImpl();
        mModel.onCreate(mContext, soundData);
        if (mView != null) {
            mView.setAdapter(this);
        }

        if(mCurrentSelectedItem == null) {
            if(mModel.getCount()!=0) {
                mCurrentSelectedItem = mModel.getItem(0);
                mModel.getItem(0).setEnable(true);
                notifyDataSetChanged();
            }
        }
        notifyDataSetChanged();
    }

    @Override
    public SDCardSoundRecycleView.SoundItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return mView.onCreateViewHolder(parent, viewType);
    }

    @Override
    public void onBindViewHolder(SDCardSoundRecycleView.SoundItemViewHolder holder, int position) {
        final SoundData soundData = mModel.getItem(position);
        holder.mItemBtn.setText(soundData.getTitle());
        holder.mItemBtn.setSelected(soundData.isEnable());
        holder.mItemBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemClick(soundData);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mModel.getCount();
    }
    /**
     * This method is used to handle logic when click on a sound item on list.
     *
     * @param item
     */
    private void onItemClick(SoundData item) {
        for (SoundData soundData : mModel.getListSound()) {
            if (soundData.isEnable()) {
                soundData.setEnable(false);
                break;
            }
        }
        mCurrentSelectedItem = item;
        item.setEnable(true);
        notifyDataSetChanged();
        playSound(mCurrentSelectedItem.getSoundUri());
    }


    @Override
    public SoundData getCurrentSelectedItem() {
        return mCurrentSelectedItem;
    }

    @Override
    public void onDestroyView() {
        stopRingtone();
    }

    @Override
    public boolean addSoundItem(SoundData soundData) { return true;}

    /**
     * This method is used to play the sound.
     *
     * @param soundPath
     */
    private void playSound(String soundPath) {
        stopRingtone();
        try {
            mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
            mAudioManager.setStreamVolume(AudioManager.STREAM_ALARM, mAudioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM), AudioManager.FLAG_PLAY_SOUND);
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setDataSource(mContext, Uri.parse(soundPath));
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
            mMediaPlayer.setLooping(true);
            mMediaPlayer.prepareAsync();
            mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mediaPlayer) {

                    mediaPlayer.start();
                }
            });
        } catch (Exception e) {
            mMediaPlayer = null;
        }
    }

    /**
     * This method is used to stop the sound which is playing.
     */
    private void stopRingtone() {
        if (mMediaPlayer != null) {
            mMediaPlayer.stop();
        }
    }
}